package com.etc.service.impl;

import com.etc.dao.*;
import com.etc.entity.*;
import com.etc.service.DetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class DetailServiceImpl implements DetailService {


    @Autowired
    private DetailMapper detailMapper;

    @Autowired
    private DrugMapper drugMapper;

    @Autowired
    private PayMapper payMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private PatientMapper patientMapper;

    @Override
    public boolean addDetail(String content,String orderId) {
        String payId = UUID.randomUUID().toString().replace("-", ""); //UUID
        System.out.println(content);  //3盒碳酸钙D3片,3,5盒倍他乐克 酒石酸美托洛尔片,5,
        String[] splitContent = new String[0];
        String detailContent = "";
        int number;
        String drugName = "";
        String drugContent = "";
        BigDecimal price;
        BigDecimal drugsum = new BigDecimal("0");
        BigDecimal drugPrice = new BigDecimal("0");


        splitContent = content.split(","); //{3盒碳酸钙D3片:3},{5盒倍他乐克 酒石酸美托洛尔片:5}
        for (int i = 0; i < splitContent.length; i++) {
            detailContent = splitContent[i];  //当i=0时{3盒碳酸钙D3片:3}
            Detail detail = new Detail();
            String[] split = new String[0];
            if (detailContent.contains("盒")) {
                split = detailContent.split("盒");//{3},{碳酸钙D3片},{3}
            } else if (detailContent.contains("片")) {
                split = detailContent.split("片");
            } else if (detailContent.contains("瓶")) {
                split = detailContent.split("瓶");
            }

            for (int j = 0; j < split.length; j++) {
                String[] split1 = new String[0];
                number = Integer.parseInt(split[0]);  //3
                drugContent = split[1];  //碳酸钙D3片:3
                split1 = drugContent.split(":");
                drugName = split1[0];//碳酸钙D3
                List<Drug> drugDetail = drugMapper.findDrugDetail(drugName);
                for (Drug drug : drugDetail) {
                    price = drug.getPrice();
                    detail.setPayId(payId);
                    detail.setContent(detailContent);  //
                    for (int k = 0; k < number; k++) {
                        drugPrice = drugPrice.add(price);
                    }
                    detail.setMoney(drugPrice);
                    drugsum = drugsum.add(drugPrice);
                    detailMapper.insertDetail(detail);
                    break;
                }
                break;
            }
        }
        Pay pay = new Pay();
        pay.setPayId(payId);
        pay.setCreateTime(LocalDate.now().toString());
        pay.setMoney(drugsum);
        pay.setOrderId(orderId);
        pay.setStatus(0);
        payMapper.insertPayDetail(pay);
        return true;
    }

    @Override
    public boolean deleteDetail(String patientId) {
        List<Order> orderByPatientId = orderMapper.findOrderByPatientId(patientId);
        Pay pay=new Pay();
        pay.setOrderId(orderByPatientId.get(0).getOrderId());
        List<Pay> payDetail = payMapper.findPayDetail(pay);
        int i = detailMapper.deleteDetail(0, payDetail.get(0).getPayId());


        return i>0;
    }
}
